/**
 * 权限设置窗口
 * SelectRightWin.show(record.roleId,function(){
		store.load();
	});
 */
var SelectRightWin = (function(){
	
	var listTopMenuUrl = ctx + 'listTopMenu.do'
	var listMenuUrl = ctx + 'listMenuByTabId.do';
	var listRolePermissionByRoleIdUrl = ctx + 'listRolePermissionByRoleId.do';
	var addRolePermissionUrl = ctx + 'addRolePermission.do';
	var delRolePermissionUrl = ctx + 'delRolePermission.do';
	
	var inited = false;
	var tab = null;
	var win = null;
	var tree = null;
	var treeStore;
	var grid = null;
	var gridStore;
	
	var curRoleId,curCallback;
	var curSfIds;
	
	var html = [
		'<div id="rightSelectDialog" class="hide">'
			,'<div id="srw_tab"></div>'
			,'<div class="tab-body">'
				,'<table>' 
					,'<tr>' 
						,'<td id="srw_treeMenu" valign="top"></td>' 
						,'<td id="srw_gridOpt" valign="top" style="padding-left:20px;"></td>' 
					,'<tr>' 
				,'</table>' 
			,'</div>'
		,'</div>'
	];
	
	function init(roleId,callback) {
		curCallback = callback;
		
		var sfIds = getData(roleId);
		
		setValue(roleId,sfIds);
		
		if(!inited) {
			inited = true;
			
			initPanel();
		}
	}
	
	function getData(roleId) {
		var sfIds = [];
		Action.postSync(listRolePermissionByRoleIdUrl,{roleId:roleId},function(sysFuns) {
			for(var i=0,len=sysFuns.length;i<len;i++){
				sfIds.push(sysFuns[i].sfId);	
			}
		});
		return sfIds;
	}
	
	function initPanel() {
		var panel = html.join('');
		$('body').append(panel);
		
		initTab();
		
		initTree();
		
		initGrid();
		
		initDialog();
	}
	
	function initTab() {
		var store = new BUI.Data.Store({url:listTopMenuUrl,autoLoad:true});
		store.on('beforeprocessload',function(e){
			var rows = e.data;
			var items = [];
			for(var i=0; i<rows.length; i++) {
				var row = rows[i];
				items.push({text:row.tabName,value:row.id});
			}
			
			tab = new BUI.Tab.TabPanel({
	            render : '#srw_tab'
		        ,elCls : 'nav-tabs'
		        ,autoRender: true
				,children:items			       
	        });
	        
	        tab.on('itemselected',function(e){
	        	var item = e.item;
	        	treeStore.load({tabId:item.get('value')});
	        });
	        
	        tab.setSelected(tab.getFirstItem());
		});
	}
	
	function initDialog() {
		
		win = new BUI.Overlay.Dialog({
			contentId:'rightSelectDialog'
			,title:'设置权限'
			,width:650
			,height:490
			,mask:true  
			,buttons:[
				 { text:'关闭', elCls : 'button', handler : function(){ 
				 	this.close(); 
				 	} 
				 }
			]
		});
		
		win.on('closed',function(){
			curCallback();
		});
	}
	
	function initTree() {
		
		treeStore = new BUI.Data.TreeStore({
	        //返回的数据如果数据有children字段，且children.length == 0 ，则认为存在未加载的子节点
	        //leaf = false，没有children字段也会认为子节点未加载，展开时会自动加载
	        url: listMenuUrl,
			map : {
		      'srId' : 'id'
		      ,'resName' : 'text'
		    }
		    ,showLine : true //显示连接线
	    });
	    
	    tree = new BUI.Tree.TreeList({
	        render: '#srw_treeMenu',
	        showLine: true,
	        width:250,
	        height: 350,
	        expandEvent : 'itemclick', //单击展开节点
	        collapseEvent : 'itemclick',  //单击收缩节点
	        itemTplRender:function(item){
	        	var count = getOperateCount(item);
	        	var span = count > 0 
		        	? '<strong style="color:red;margin-left:5px;">'+count+'</strong>'
		        	: '';
		        	
	        	return '<li>' + item.text + span + '</li>'
	        },
	        store: treeStore
	    });
	    
	    tree.on('selectedchange',function(ev){
	    	var node = ev.item;
	    	gridStore.setResult(node.sysFuns);
	    });
	    
	    tree.render();
	}
	
	function initGrid() {
		var Grid = BUI.Grid;
		var columns = [
			{title : '权限功能',dataIndex :'operateName',sortable : false}
		];
		
		gridStore = new BUI.Data.Store();
		
		gridStore.on('beforeprocessload',function(e){
			var rows = e.data;
			for(var i=0;i<rows.length;i++) {
				var row = rows[i];
				var sfId = row.sfId;
				for(var j=0; j<curSfIds.length; j++) {
					row.selected = false;
					if(sfId == curSfIds[j]) {
						row.selected = true;
						break;
					}
				}
			}
			return rows;
		});
		
		grid = new Grid.Grid({
		    render: '#srw_gridOpt',
		    idField: 'sfId',
		    columns: columns,
		    forceFit: true,
		    width: 250,
		    height: 320,
		    emptyDataTpl: '<h4 class="centered">无数据</h4>',
		    store: gridStore,
		    itemStatusFields: {
		        selected: 'selected',
		        disabled: 'disabled'
		    },
		    plugins: [Grid.Plugins.CheckSelection]
		});
        
        grid.on('itemselected',function(e){
        	var item = e.item;
        	if(item){
		        addRolePermission(item.sfId);
        	}
        });
        
        grid.on('itemunselected',function(e){
        	var item = e.item;
        	if(item){
		        delRolePermission(item.sfId);
        	}
        });
        
      	grid.render();
	}
	
	function getOperateCount(item) {
		var count = 0;
    	var sysFuns = item.sysFuns || [];
    	for(var i=0; i<sysFuns.length; i++) {
    		var sfId = sysFuns[i].sfId
    		for(var j=0; j<curSfIds.length; j++) {
    			if(sfId == curSfIds[j]) {
    				count++;
    			}
    		}
    		
    	}
    	
    	var children = item.children || [];
    	for(var i=0; i<children.length; i++) {
    		var child = children[i];
    		count +=getOperateCount(child);
    	}
    	
    	return count;
	}
	
	function addRolePermission(sfId) {
		setRolePermission({
			url:addRolePermissionUrl
			,sfId:sfId
			,roleId:curRoleId
		});
	}
	
	function delRolePermission(sfId) {
		setRolePermission({
			url:delRolePermissionUrl
			,sfId:sfId
			,roleId:curRoleId
		});
	}
	
	function setRolePermission(param) {
		var url = param.url;
		
		var data = {
			sfId:param.sfId
			,roleId:param.roleId
		}
		
		Action.post(url,data,function(e){
			Action.execResult(e);
		});
	}
	
	function reset() {
		gridStore.setResult([]);
		if(tab) {
			tab.clearSelected();
			tab.setSelected(tab.getFirstItem());
		}
	}
	
	function setValue(roleId,sfIds) {
		curRoleId = roleId;
		curSfIds = sfIds;
	}
	
	return {
		show:function(roleId,callback) {
			init(roleId,callback);
			
			reset();
			
			win.show();
		}
		,hide:function() {
			win.hide();
		}
	};
	
	
})();
